var parentJson = {};
parentJson.limit=1000;
parentJson.headField="pno,content,enddate,plantype,mno,ino,commituserid,commituser,completedate,flag,jindu,replycontent,img";
parentJson.page=1;
var enddatecalendar = new lCalendar();
var completedatecalendar = new lCalendar();
var url  = location.search;
var theRequest = new Object();  
if ( url.indexOf( "?" ) != -1 ){  
var str = url.substr( 1 ); //substr()方法返回从参数值开始到结束的字符串；  
var strs = str.split( "&" ); 
var plantype=1; 
var mno;
var ino;
for ( var i = 0; i < strs.length; i++ ) {  
theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );  
}  
}
var aa={};
aa.conditionField = "pno";
aa.conditionValue = theRequest.pno;
aa.conditionOption = "=";
aa.logicalOperator = "and";
parentJson.search=aa;
var now=getNowFormatDate();
var _config ;
var photoUrl = [];//拍照列表
var now = getDay(0);
var theusername = sessionStorage.getItem("name");
var theuserid = sessionStorage.getItem("userId");
$.ajax({
	url: "../../work_plan_info/getSelectBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(parentJson) 
	,dataType:'json'
	,success: function(res){
		console.log(res.data[0]);
		plantype=res.data[0].plantype;
		mno=res.data[0].mno;
		ino=res.data[0].ino;
		if(res.data[0].content==undefined){
			content.value="";
		}else{content.value=res.data[0].content;}
		if(res.data[0].enddate==undefined){
			enddate.value="";
		}else{
			enddate.value=res.data[0].enddate;
		}
	/*	if(res.data[0].completedate==undefined){
			var date = new Date(); 
			var hour = date.getHours();
		    var minute = date.getMinutes();
		    var second = date.getSeconds();
		    hour = hour < 10 ? ("0" + hour) : hour;
		    minute = minute < 10 ? ("0" + minute) : minute;
		    second = second < 10 ? ("0" + second) : second;
    		var Timer = hour + ':' + minute + ':' + second;
			completedate.value=now+' '+Timer;
		}else{
			completedate.value=res.data[0].completedate;
		}*/
	/*	if(res.data[0].flag==undefined){
			flag.value="";
		}else{
			flag.value=res.data[0].flag;
		}*/
		/*if(res.data[0].completedate==undefined){
			completedate.value="";
		}else{
			completedate.value=res.data[0].completedate;
		}*/
		if(res.data[0].jindu==undefined){
			jindu.value="";
		}else{
			jindu.value=res.data[0].jindu;
		}
		if(res.data[0].replycontent==undefined){
			replycontent.value="";
		}else{
			replycontent.value=res.data[0].replycontent;
		}
		if(res.data[0].commituserid==undefined){
			commituserid0.value="";
		}else{
			commituserid0.value=res.data[0].commituserid;
		}
		if(res.data[0].commituser==undefined){
			commituser0.value="";
		}else{
			commituser0.value=res.data[0].commituser;
		}
		
													if(res.data[0].img==undefined||res.data[0].img==""){
					                                     console.log("null");
				                                    }else{
														photoUrl=[];
														photoUrl.push(res.data[0].img);	
														var result=res.data[0].img.split(",");	
														for(var i=0;i<result.length;i++){
												  
															$('#wig_img').append('<div class="img_box" id="img_box'+i+'">'
															+'<img alt="100x100" src="'+result[i]+'" onclick="showAllPic(\''+result[i]+'\')"/>'
															+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
															+'</div>'
															);
														}
													}

	
	}
});
$(document).ready(function(){
	$('#name').text(sessionStorage.getItem("name"));
	$('#searchDate').text(now);
	$('#saveButton').on('click',saveInfo);
		$(document).on('click','input[jsonKey="commituser"]',function(){
		var valueInput = $(this).attr('jsonKey')+'id'+$(this).attr('index');
		var nameInput = $(this).attr('jsonKey')+$(this).attr('index');
		var index = layer.open({
			type: 2,
			maxmin: true,
			title: '选择人员',
			area: ["300px", "600px"],
			content: '../adminmanager/toUsersTree?valueInput='+valueInput+'&nameInput='+nameInput,
			btn: ['确定', '取消'] ,
			yes: function(index, layero){
			    //do something
			    layer.close(index); //如果设定了yes回调，需进行手工关闭
			},
			btn2: function(index, layero){
			    //do something
			    layer.close(index); //如果设定了yes回调，需进行手工关闭
			},
			success: function(layero, index){
			}
		});
		layer.full(index);
//		setUserXmselectOne([],$(this).attr('index'),$(this).attr('jsonKey'))
	});
	
	
});
$('#paizhao').on('click',function(){
	openphoto();
});
/*
enddatecalendar.init({
	'trigger': '#enddate',
	'type': 'datetime',
	'maxDate': '2099-12-31 23:59:59',
	'minDate': now
});*/
/*completedatecalendar.init({
	'trigger': '#completedate',
	'type': 'datetime',
	'maxDate': '2099-12-31 23:59:59',
	'minDate': now
});*/
function ICalendarClick(){
	
}	
function saveInfo(){
if($('#jindu').val()==""){alert("完成进度为必填项");}
else if($('#replycontent').val()==""){alert("结果说明为必填项");}else{
	showloading();
	try {
		var imgStr = "";
		var sendData = {};
		sendData.pno=theRequest.pno;
		sendData.plantype=plantype;
		sendData.mno=mno;
		sendData.ino=ino;
		sendData.content = $('#content').val();
		sendData.enddate = $('#enddate').val();
		sendData.completedate = $('#completedate').val();  //完成时间  共用
		sendData.flag = $('#flag').val();                  //状态      共用
		sendData.jindu = $('#jindu').val();
		sendData.replycontent = $('#replycontent').val();   //回复内容 /结果说明  共用
		sendData.img = photoUrl.join(',');                  //图片    公用
		//sendData.meetinguserid =  $('#meetinguserid0').val();
		//sendData.meetinguser =  $('#meetinguser0').val();
		$('div[itemDiv]').each(function(i,obj){
				$(obj).find('input[jsonKey]').each(function(j,el){
					if($(el).parent().find('span').text() == '*' && $(el).val() == ''){
						alert($(el).parent().find('h5').text()+"为必填项");
						checkFlag = false;
						return false;
					}else{
						var key = $(el).attr('jsonKey');
						sendData[key] = $(el).val();
						key = null;
					}
				});

		});
				
		sendData.meetingitem=$('#content').val();
		sendData.replydate= $('#enddate').val().substring(0,10);;
 		sendData.username=theusername;
		sendData.xgr=theuserid; 
		console.log(sendData);
					$.ajax({
					url: "../../work_plan_info/mobileedit"
					,type:'post'
					,dataType:'json'
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(sendData) 
					,success: function(res){
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg(res.msg,{time:1000},function(){
							history.go(-1);
						});
					}
					}
				    });
	
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}
	}	
}	
//获取当前日期
function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			photoUrl.push(result[0]);
			$('#paizhapoUrl .clearfix').remove()
			$('#paizhapoUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#paizhapoUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		photoUrl.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: photoUrl,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
